///*
// *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
// *
// *  Redistribution and use in source and binary forms, with or without
// *  modification, are permitted provided that the following conditions are met:
// *
// *  Redistributions of source code must retain the above copyright notice,
// *  this list of conditions and the following disclaimer.
// *  Redistributions in binary form must reproduce the above copyright
// *  notice, this list of conditions and the following disclaimer in the
// *  documentation and/or other materials provided with the distribution.
// *  Neither the name of the dreamlu.net developer nor the names of its
// *  contributors may be used to endorse or promote products derived from
// *  this software without specific prior written permission.
// *  Author: Chill 庄骞 (smallchill@163.com)
// */
//package com.plian.system.controller.fr.evaluation;
//
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.plian.Tools.*;
//import com.plian.Tools.base.BaseController;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.common.status.CustomCode;
//import com.plian.system.config.Resubmit;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.entity.fr.evaluation.AssetEvaluationA;
//import com.plian.system.entity.fr.evaluation.AssetEvaluationB;
//import com.plian.system.entity.fr.evaluation.EvaluationApproval;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ListResult;
//import com.plian.system.model.ObjResult;
//import com.plian.system.mybatis.search.SearchEntity;
//import com.plian.system.mybatis.search.SearchParamEntiy;
//import com.plian.system.mybatis.search.TokenSearch;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.mybatis.support.Query;
//import com.plian.system.service.fr.evaluation.IAssetEvaluationAService;
//import com.plian.system.service.fr.evaluation.IAssetEvaluationBService;
//import com.plian.system.service.fr.evaluation.IEvaluationApprovalService;
//import com.plian.system.vo.fr.evaluation.AssetEvaluationAVO;
//import com.plian.system.wrapper.fr.evaluation.AssetEvaluationAWrapper;
//import com.plian.system.wrapper.fr.evaluation.AssetEvaluationBWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.validation.Valid;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///**
// * 资产评估项目审理主表 控制器
// *
// * @author jianglei
// * @since 2019-10-17
// */
//@Slf4j
//@RestController
//@AllArgsConstructor
//@RequestMapping("api/assetevaluationa")
//public class AssetEvaluationAController extends BaseController {
//
//	private IAssetEvaluationAService assetEvaluationAService;
//
//	private IAssetEvaluationBService assetEvaluationBService;
//
//	private IEvaluationApprovalService evaluationApprovalService;
//
//	private AssetEvaluationAWrapper assetEvaluationAWrapper;
//
//	private AssetEvaluationBWrapper assetEvaluationBWrapper;
//
//
//    /**
//    * 详情
//    */
//	@PostMapping("/detail")
//	public ObjResult detail(@Valid @RequestBody AssetEvaluationA assetEvaluationA) {
//		ObjResult objResult;
//
//		try{
//			AssetEvaluationA detail = assetEvaluationAService.getOne(Condition.getQueryWrapper(assetEvaluationA));
//			objResult = new ObjResult(CommonCode.SUCCESS);
//			if (Optional.ofNullable(detail).isPresent()){
//				PageData pd = new PageData();
//				AssetEvaluationB assetEvaluationB = new AssetEvaluationB();
//				assetEvaluationB.setAssetEvaluationId(detail.getId());
//				pd.put("assetEvaluationBS", assetEvaluationBWrapper.entityToVO(assetEvaluationBService.list(Condition.getQueryWrapper(assetEvaluationB))));
//
//				AssetEvaluationAVO assetEvaluationAVO = assetEvaluationAWrapper.entityToVO(detail);
//
//				ObjectMapper mapper = new ObjectMapper();
//				String json = JSON.toJSONStringWithDateFormat(assetEvaluationAVO, DateUtil.PATTERN_DATETIME);
//				pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String, String>>(){}));
//				objResult.setPageData(pd);
//			}
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//			objResult = new ObjResult(CommonCode.ERROR);
//			return objResult;
//		}
//
//		return objResult;
//
//	}
//
//	/**
//	* 分页 资产评估项目审理主表
//	*/
//	@PostMapping("/list")
//	public ListResult<PageData> list(@Valid @RequestBody Query query) {
//		List<SearchEntity> entityList = query.getSearch();
//
//		if (query.getAscs()==null&&query.getDescs()==null){
//			query.setDescs("create_time");
//		}
//
//		QueryWrapper<AssetEvaluationA> queryWrapper = SearchParamEntiy.getQueryWrapper(Condition.getQueryWrapper(new AssetEvaluationA()), entityList);
//		TokenSearch<AssetEvaluationA> tokenSearch = new TokenSearch<>();
//		tokenSearch.addTokenQuery(queryWrapper, query);
//		IPage<AssetEvaluationA> pages = assetEvaluationAService.page(Condition.getPage(query), queryWrapper);
//		List<AssetEvaluationAVO> assetEvaluationAVOS = assetEvaluationAWrapper.entityToVO(pages.getRecords());
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(assetEvaluationAVOS)){
//				List<EvaluationApproval> evaluationApprovals = evaluationApprovalService.list();
//				List<Long> ids = new ArrayList<>();
//				if (CollectionUtil.isNotEmpty(evaluationApprovals)) {
//					ids = evaluationApprovals.stream().filter(evaluationApproval -> evaluationApproval.getUpstreamCodeId() != null)
//							.map(EvaluationApproval::getUpstreamCodeId).collect(Collectors.toList());
//				}
//				for (AssetEvaluationAVO vo : assetEvaluationAVOS){
//					PageData pd = new PageData();
//					ObjectMapper mapper = new ObjectMapper();
//					if (CollectionUtil.isNotEmpty(ids) && ids.contains(vo.getId())){
//						vo.setFlag(2);
//					}else {
//						vo.setFlag(1);
//					}
//					String json = JSON.toJSONStringWithDateFormat(vo, DateUtil.PATTERN_DATETIME);
//					pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String, String>>(){}));
//					pageDataList.add(pd);
//				}
//			}
//			ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
//			listResult.setList(pageDataList);
//			listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//			return listResult;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//			return new ListResult<>(CommonCode.ERROR);
//		}
//	}
//
//
//
//	/**
//	* 新增或修改 资产评估项目审理主表
//	*/
//	@PostMapping("/submit")
//	@Resubmit(delaySeconds = 0)
//	public ObjResult submit(@Valid @RequestBody AssetEvaluationA assetEvaluationA) {
//		if (!(Optional.ofNullable(assetEvaluationA).isPresent() && Optional.ofNullable(assetEvaluationA.getStatus()).isPresent()
//				&& assetEvaluationA.getStatus() == -1)) {
//			if (!StringUtil.isEmpty(assetEvaluationA.getCode())) {
//				if (!assetEvaluationAService.checkIsExisted(assetEvaluationA.getId(), assetEvaluationA.getCode())) {
//					return new ObjResult(CustomCode.builder().message("单据编号已存在").code(1999).success(false).build());
//				}
//			}
//
//			if (assetEvaluationA.getAssetEvaluationBS().size() <= 0) {
//				return new ObjResult(CustomCode.builder().message("资产评估项目审理子表信息未填").code(1999).success(false).build());
//			}
//		}
//
//		return assetEvaluationAService.submit(assetEvaluationA)
//				? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
//
//	}
//
//
//
//
//
//	/**
//	* 删除 资产评估项目审理主表
//	*/
//	@PostMapping("/remove")
//	public ObjResult remove(@RequestBody PageData pd) {
//		ObjResult objResult;
//		List<String> ids = (List<String>)pd.get("ids");
//		if (assetEvaluationAService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))){
//			objResult = new ObjResult(CommonCode.SUCCESS);
//			return objResult;
//		}else {
//			objResult = new ObjResult(CommonCode.FAIL);
//		}
//		return objResult;
//	}
//
//
//	/**
//	 * 变更 资产评估项目审理主表
//	 */
//	@PostMapping("/change")
//	public ObjResult change(@Valid @RequestBody AssetEvaluationA assetEvaluationA) {
//		ObjResult objResult;
//		if (assetEvaluationAService.change(assetEvaluationA)){
//			objResult = new ObjResult(CommonCode.SUCCESS);
//			return objResult;
//		}else {
//			objResult = new ObjResult(CommonCode.FAIL);
//		}
//		return objResult;
//	}
//
//	// todo excel 先不处理
////	/**
////	 * 输出excel前置
////	 */
////	@PostMapping("/preexport")
////	public R<String> preexport(@RequestBody String jsonString){
////		String md5= DigestUtil.hmacMd5Hex(jsonString, SecureUtil.getUserId() +"");
////		File file=assetEvaluationAService.exportExcel(JSON.parseObject(jsonString));
////		return  R.data(AssetEvaluationACache.putJsonByKey(md5,file));
////	}
////	/**
////	 * 输出excel
////	 */
////	@GetMapping("/export/{key}")
////	public ResponseEntity<FileSystemResource> export(@PathVariable String key) {
////		return FileTools.exportFile(AssetEvaluationACache.getJsonByKey(key));
////	}
//
//	/**
//	 * 分页 用于资产评估核准与备案维护添加按钮获取上游单据
//	 */
//	@PostMapping("/getPreList")
//	public ListResult<PageData> getPreList(@Valid @RequestBody Query query) {
//
//		if (query.getAscs()==null&&query.getDescs()==null){
//			query.setAscs("code");
//		}
//
//		QueryWrapper<AssetEvaluationA> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("status", FormStatusConstant.FINISH);
//		queryWrapper.eq("report_company_id", TokenUtil.getTokenOrgId());
//		/**
//		 * 排除已经被使用的数据
//		 */
//		List<EvaluationApproval> evaluationApprovals = evaluationApprovalService.list();
//		if (CollectionUtil.isNotEmpty(evaluationApprovals)){
//			List<Long> ids = evaluationApprovals.stream().filter(evaluationApproval -> evaluationApproval.getUpstreamCodeId() != null)
//				.map(EvaluationApproval::getUpstreamCodeId).collect(Collectors.toList());
//			if (CollectionUtil.isNotEmpty(ids)){
//				queryWrapper.notIn("id",ids);
//			}
//
//		}
//
//		IPage<AssetEvaluationA> pages = assetEvaluationAService.page(Condition.getPage(query), queryWrapper);
//
//		List<AssetEvaluationAVO> assetEvaluationAVOS = assetEvaluationAWrapper.entityToVO(pages.getRecords());
//
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(assetEvaluationAVOS)){
//				for (AssetEvaluationAVO vo : assetEvaluationAVOS){
//					PageData pd = new PageData();
//					ObjectMapper mapper = new ObjectMapper();
//					String json = JSON.toJSONStringWithDateFormat(vo, DateUtil.PATTERN_DATETIME);
//					pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String,String>>(){}));
//					pageDataList.add(pd);
//				}
//			}
//			ListResult<PageData> listResult = new ListResult<>(CommonCode.SUCCESS);
//			listResult.setList(pageDataList);
//			listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
//			return listResult;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//			return new ListResult<>(CommonCode.ERROR);
//		}
//	}
//}
